package com.chatcompetition.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chatcompetition.common.api.CommonPage;
import com.chatcompetition.context.BaseContext;
import com.chatcompetition.domain.entity.CompetitionActivity;
import com.chatcompetition.mapper.ActivityMapper;
import com.chatcompetition.service.ActivityService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, CompetitionActivity> implements ActivityService {

    @Autowired
    private ActivityMapper activityMapper;

    @Override
    public CompetitionActivity getById(Long id) {
        return getById(id);
    }
    @Override
    public boolean update(CompetitionActivity activity) {
        return updateById(activity);
    }

    @Override
    public boolean delete(Long id) {
        return removeById(id);
    }

    @Override
    public List<String> convertCompetitionsToActivities() {
        // 创建查询条件，查询类别为“竞赛”的记录
        QueryWrapper<CompetitionActivity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category", "活动");

        // 查询所有符合条件的记录
        List<CompetitionActivity> competitionActivities = list(queryWrapper);

        // 提取所有下分类别，并去重
        List<String> subCategories = competitionActivities.stream()
                .map(CompetitionActivity::getSubCategory)
                .distinct()
                .collect(Collectors.toList());

        return subCategories;
    }



    @Override
    public CommonPage<CompetitionActivity> listAllActivities(int page, int size) {
        // 使用 PageHelper 进行分页
        PageHelper.startPage(page, size);

        // 查询条件：查询当前用户添加的活动
        QueryWrapper<CompetitionActivity> wrapper = new QueryWrapper<>();
        wrapper.eq("category", "活动");

        // 查询所有活动
        List<CompetitionActivity> list = activityMapper.selectList(wrapper);

        // 将分页结果封装到 CommonPage
        return CommonPage.restPage(list);
    }

    @Override
    public CommonPage<CompetitionActivity> listAllActivitiesAddTeachers(int page, int size) {
        String employeeNumber = BaseContext.getCurrentId(); // 获取当前用户的工号

        // 使用 PageHelper 进行分页
        PageHelper.startPage(page, size);

        // 查询条件：查询当前用户添加的活动
        QueryWrapper<CompetitionActivity> wrapper = new QueryWrapper<>();
        wrapper.eq("employee_number", employeeNumber)
               .eq("category", "活动");

        // 执行查询
        List<CompetitionActivity> list = activityMapper.selectList(wrapper);

        // 将分页结果封装到 CommonPage
        return CommonPage.restPage(list);
    }









    @Override
    public List<CompetitionActivity> listAllActivities() {
        // 查询类别为 "活动" 的记录
        QueryWrapper<CompetitionActivity> wrapper = new QueryWrapper<>();
        wrapper.eq("category", "活动");
        return activityMapper.selectList(wrapper);
    }

    @Override
    public List<CompetitionActivity> listAllActivitiesAddTeachers() {

        String employeeNumber = BaseContext.getCurrentId();
        // 查询类别为 "活动" 的记录
        QueryWrapper<CompetitionActivity> wrapper = new QueryWrapper<>();
        wrapper.eq("category", "活动")
                .eq("employee_number", employeeNumber);
        return activityMapper.selectList(wrapper);
    }
}
